package com.cheyun.netsalev3.view.home


import android.os.Bundle
import android.util.Log
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager

import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.FragmentHomeViewPageBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.viewmodel.HomeTaskFragment2ViewModel
import com.jeremyliao.liveeventbus.LiveEventBus


class HomeViewPageFragment : BaseFragment<FragmentHomeViewPageBinding>() {
    override fun initContentView(): Int {
        return R.layout.fragment_home_view_page
    }

    //当前位置
    var pos = 0
    lateinit var adapter: MyAdapter
    lateinit var adapter2: MyAdapter


    override fun initViewModel(): ViewModel? {
        parentFragment?.apply {
            return ViewModelProviders.of(this).get(HomeTaskFragment2ViewModel::class.java)
        }
        return null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            pos = it.getInt("pos")
        }
    }

    override fun initView() {
        adapter = MyAdapter(R.layout.task_adapter_item, binding.viewModel)
        binding.recyclerViewList.layoutManager = LinearLayoutManager(context)
        binding.recyclerViewList.adapter = adapter
        adapter2 = MyAdapter(R.layout.task_adapter_item, binding.viewModel)
        binding.recyclerViewList2.layoutManager = LinearLayoutManager(context)
        binding.recyclerViewList2.adapter = adapter2
        binding.recyclerViewList.isNestedScrollingEnabled = false
        binding.recyclerViewList2.isNestedScrollingEnabled = false
        binding.homeScrollView.setOnScrollChangeListener(object:NestedScrollView.OnScrollChangeListener{
            override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
                LiveEventBus.get().with("enableScroll").postValue(scrollY!=0)
                Log.i("scroll",scrollY.toString())
            }
        })
    }

    override fun initData() {
        binding.viewModel?.dataList!![pos]?.observe(this, Observer { item ->
            Log.e("viewModel", pos.toString())
            if(item != null) {
                adapter.submitList(item.arrive_data)
                adapter2.submitList(item.connect_data)

                binding.viewModel?.currentDaodian = item.arrive_data.size
                binding.viewModel?.currentLianxi = item.connect_data.size

                binding.txtlianxi.setText("( " + item.connect_data.size + " 条)");
                binding.txtdaodian.setText("( " + item.arrive_data.size + " 条)");
            }
        })

    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit" + ","+ pos)
        binding.viewModel?.loadData(pos)
    }

    companion object {
        @JvmStatic
        fun newInstance(pos: Int) =
                HomeViewPageFragment().apply {
                    arguments = Bundle().apply {
                        putInt("pos", pos)
                    }
                }
    }
}
